package com.demo.SocketServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import org.apache.commons.io.IOUtils;

public class BIOClinet {

	public static String connect(String host, int port, byte[] message) {
		String serverinfo = null;
		try {
			// 1.创建客户端Socket，指定服务器地址和端口号
			Socket socket = new Socket(host, port);
			// 2.获取输出流，用来向服务器发送信息
			OutputStream os = socket.getOutputStream();// 字节输出流
			os.write(message);
			os.flush();
			// 关闭输出流
			socket.shutdownOutput();
			// 3.获取输入流，用来读取服务器端的响应信息
			InputStream is = socket.getInputStream();
			serverinfo = IOUtils.toString(is, "UTF-8");
			// 4.关闭资源
			is.close();
			os.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return serverinfo;
	}

}
